package com.yrd.fssync.webui.servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yrd.common.webui.HttpEngine;
import com.yrd.common.webui.ResponseMessage;
import com.yrd.fssync.core.FileSystemMonitor;
import com.yrd.fssync.core.FileSystemMonitorManager;
import com.yrd.fssync.core.SyncEngine;
import com.yrd.fssync.core.SyncRequest;
import com.yrd.fssync.core.Utils;

public class Query {

	
	public synchronized void index(HttpServletRequest request, HttpServletResponse reponse) throws UnsupportedEncodingException, IOException{
		FileSystemMonitorManager fsmanager = Utils.getContext().getFileSystemMonitorManager();
		
		JSONObject index = new JSONObject();
		JSONArray monitorArr = new JSONArray();
		for(FileSystemMonitor monitor : fsmanager.getFileSystemMap().values()){
			JSONObject jsmonitor = new JSONObject();
			jsmonitor.put("id", monitor.getId());
			JSONArray engineArr = new JSONArray();
			jsmonitor.put("engines", engineArr);
			for(SyncEngine engine : monitor.getSyncEngineMap().getMap().values()){
				engineArr.add(engine.getId());
			}
			monitorArr.add(jsmonitor);
		}
		index.put("pairs", monitorArr);
		ResponseMessage msg = ResponseMessage.success("成功");
		msg.setData(index);
		String msgStr = JSON.toJSONString(msg);
		reponse.getOutputStream().write(msgStr.getBytes("utf-8"));
	}
	
	
}
